<template>
  <div class="list-wrap">
    <div class="list">
      <p class="tit">
        <span class="space"></span>
        <span class="No">序号</span>
        <span class="text-left sp2" style="min-width: 150px">论文标题</span>
        <span class="text-left sp2" style="min-width: 150px">出版物</span>
        <span class="text-left sp2" style="min-width: 150px">关键词</span>
        <span class="text-left sp2" style="min-width: 150px">出版单位</span>
        <span class="text-left sp2" style="max-width: 60px">出版年</span>
        <span class="text-left sp2">作者</span>
      </p>
      <ul class="context">
        <el-checkbox-group v-model="checkBoxList">
          <li
            v-for="(item, index) in this.listData"
            :key="index"
            @click="toDetail(item)"
          >
            <p class="space">
              <el-checkbox
                class="list-checkbox"
                :label="item"
                >{{
              }}</el-checkbox>
            </p>
            <p class="No">{{ index + 1 }}</p>
            <p class="text-left sp2">
              <span
                v-show="defaultTrans === true"
                v-html="item.title_zh"
              ></span>
              <span v-show="defaultTrans === false" v-html="item.title"></span>
            </p>
            <p class="text-left sp2">{{ item.from_publication }}</p>
            <p class="text-left sp2" v-html="item.keywords"></p>
            <p class="text-left sp2" v-html="item.organ"></p>
            <p
              class="ipc text-left sp2"
              v-html="item.year"
              style="max-width: 60px"
            ></p>
            <p class="text-left sp2" v-html="item.author"></p>
          </li>
        </el-checkbox-group>
      </ul>
      <!-- no-data -->
      <div v-if="noData" class="no-data">
        <loading1 :emptyText="emptyText" />
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { disassembleWord, wordsLight, isZh } from "@/utils/utils.js";
export default {
  props: {
    data: {
      type: Object,
    },
    emptyText: {
      type: String,
    },
    checkAllVal: {
      type: Boolean,
    },
  },
  data() {
    return {
      listData: [],
      origListData: [],
      checkBoxList: [],
      noData: false,
    };
  },
  computed: {
    ...mapState(["searchWord", "highlightModeSettings", "defaultTrans"]),
  },
  methods: {
    handleAuthor(arr) {
      let list = [];
      arr.map((item) => {
        list.push(item.name);
      });
      return list.join("、");
    },
    getCountry(arr) {
      let country = "";
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].country) {
          country = arr[i].country;
          break;
        }
      }
      return country;
    },
    getAuthors(list) {
      let authorStr = "",
        organList = [],
        organStr = "";
      list.forEach((li, idx) => {
        if (li.org_zh || li.org) {
          if (organList.indexOf(li.org_zh || li.org) == -1) {
            organList.push(li.org_zh || li.org);
          }
        }
        let i = organList.indexOf(li.org_zh || li.org) + 1;
        authorStr += `${idx > 0 ? "，" : ""}${li.name_zh ||
          li.name}<sup>${i == 0 ? "" : i}</sup>`;
      });
      organList.forEach((list, idx) => {
        organStr += `${idx == 0 ? "" : "，"}${idx + 1}.${list}`;
      });
      return { authorStr, organStr };
    },
    getKeyWords(v) {
      if (v.hasOwnProperty("keywords_zh")) {
        return v.keywords_zh.join("，");
      } else if (v.hasOwnProperty("keywords")) {
        return v.keywords.join("，");
      } else {
        return "-";
      }
    },
    getListData(data, option = { needCollection: true }) {
      let result = data.result || [];
      let list = [];
      let origList = [];
      this.noData = false;
      result.forEach((v, index) => {
        const { authorStr, organStr } = this.getAuthors(v.authors);
        let obj = {
          ...v,
          title: v.title || v.title_ori || "-", // 标题
          title_zh: v.title_zh || v.title || "-", // 标题
          from_publication: v.venue_name || "--", //
          type: v.type || "", //
          year: v.year || "", //
          keywords: this.getKeyWords(v),
          author: authorStr,
          organ: organStr,
        };
        origList.push(obj);
      });

      // 翻译开关
      // if (this.defaultTrans) {
      //   origList.forEach((v, i) => {
      //     v.showTitTrans = false;
      //     this.transalte(v, i);
      //   });
      // } else {
      //   origList.forEach((v, i) => {
      //     v.showTitTrans = true;
      //     this.transalte(v, i);
      //   });
      // }

      this.origListData = origList;

      list = JSON.parse(JSON.stringify(origList)); // 高亮模式下的数据

      // 是否收藏
      // if (list.length > 0 && option.needCollection) {
      //   let pns = [];
      //   list.forEach((v) => {
      //     pns.push(v.pn);
      //   });

      //   this.isCollection(pns, (data) => {
      //     data.forEach((v) => {
      //       let index = origList.findIndex((b) => b.pn === v);
      //       this.$set(list[index], "isCollection", true);
      //     });
      //   });
      // }

      // 高亮模式
      let settings = [];
      let open = this.highlightModeSettings.open;
      // 默认关键词高亮样式
      let wordArr = disassembleWord(this.searchWord);
      // let defObj = wordArr;
      let defObj = [];
      if (open) {
        // 如果开启高亮模式
        settings = [...defObj, ...this.highlightModeSettings.modes];
      } else {
        settings = defObj;
      }
      list.forEach((v) => {
        v.title = wordsLight(v.title, settings);
        v.title_zh = wordsLight(v.title_zh, settings);
        v.from_publication = wordsLight(v.from_publication, settings);
        v.author = wordsLight(v.author, settings);
        v.article_year = wordsLight(v.article_year, settings);
      });

      this.listData = list;
      this.noData = origList.length <= 0 ? true : false;
    },

    // 翻译
    // transalte(item, index) {
    //   let ttl = item.ttl;
    //   if (item.showTitTrans === true) {
    //     item.showTitTrans = false;
    //   } else {
    //     // 标题
    //     if (item.transTtl && item.transTtl !== "") {
    //       item.showTitTrans = true;
    //     } else {
    //       // 判断是否为中文
    //       let ttlType = "";
    //       if (isZh(item.ttl)) {
    //         ttlType = "ttl_en";
    //       } else {
    //         ttlType = "ttl_zh";
    //       }
    //       if (item[ttlType] && item[ttlType] !== "") {
    //         this.$set(item, "transTtl", item[ttlType]);
    //         item.showTitTrans = true;
    //       } else {
    //         if (this.origListData[index] && this.origListData[index].pn) {
    //           this.transField(ttl, this.origListData[index].pn, (data) => {
    //             this.$set(item, "transTtl", data);
    //             item.showTitTrans = true;
    //           });
    //         }
    //       }
    //     }
    //   }
    // },
    // // 对应具体的翻译字段
    // transField(str, pn, func) {
    //   // 过滤一些特殊字符，否则无法正常翻译
    //   let newStr = str.replace(/&nbsp;/g, "").replace(/<br>/g, "\n"); // .replace(/[\n]/g, ' ')
    //   this.$post(this.$Url.transalte, {
    //     text: newStr,
    //     pn,
    //   })
    //     .then((res) => {
    //       let translatedObj =
    //         res.data &&
    //         res.data.translation &&
    //         res.data.translation[0] &&
    //         res.data.translation[0].translated[0];
    //       let translatedText = translatedObj.text;
    //       translatedText = translatedText.replace(/\n/g, "<br/>");
    //       func && func(translatedText);
    //     })
    //     .catch((err) => {});
    // },
    // 跳转详情
    toDetail(item) {
      let detailLeftMenu = [];
      this.origListData.forEach((v) => {
        detailLeftMenu.push({
          id: v.id || "--", // 申请号
          title: v.title, // 标题
        });
      });
      localStorage.setItem("detailLeftMenu", JSON.stringify(detailLeftMenu));

      this.$nextTick(() => {
        let routerUrl = this.$router.resolve({
          name: "DataCenterDetail",
          query: {
            id: item.id,
            type: "paper",
          },
        });
        window.open(routerUrl.href, "_blank");
      });
    },
    // 展开详情
    // showDetail(index) {
    //   let pn = this.origListData[index].pn;
    //   this.$emit("currentPn", pn);
    // },
    // 是否收藏
    // isCollection(pns, func) {
    //   this.$post(this.$Url.account.isCollection, pns)
    //     .then((res) => {
    //       let data = res.data;
    //       func && func(data);
    //     })
    //     .catch((err) => {});
    // },
    // 收藏
    // collection(index) {
    //   let pn = this.origListData[index].pn; // 从源数据中获取pn
    //   let title = this.origListData[index].ttl[0]; // 从源数据中获取title
    //   if (this.listData[index].isCollection) {
    //     // 取消收藏
    //     this.$get(this.$Url.account.cancelCollection, {
    //       pn,
    //     })
    //       .then((res) => {
    //         this.$set(this.listData[index], "isCollection", false);
    //         this.$message({
    //           type: "success",
    //           duration: 1000,
    //           message: "取消收藏成功！",
    //         });
    //       })
    //       .catch((err) => {});
    //   } else {
    //     // 收藏
    //     this.$post(this.$Url.account.addCollection, {
    //       pn,
    //       title,
    //     })
    //       .then((res) => {
    //         let data = res.data;
    //         this.$set(this.listData[index], "isCollection", data);
    //         this.$message({
    //           type: "success",
    //           duration: 1000,
    //           message: "收藏成功！",
    //         });
    //       })
    //       .catch((err) => {});
    //   }
    // },
  },
  watch: {
    data() {
      this.getListData(this.data);
    },
    // defaultTrans() {
    //   if (this.defaultTrans === true) {
    //     this.listData.forEach((v, i) => {
    //       v.showTitTrans = false; // 标题翻译
    //       this.transalte(v, i);
    //     });
    //   } else {
    //     this.listData.forEach((v, i) => {
    //       v.showTitTrans = true; // 标题翻译
    //       this.transalte(v, i);
    //     });
    //   }
    // },
    // 多选
    checkBoxList() {
      this.$Bus.$emit("getDcCheckBoxlist", this.checkBoxList);
    },
    // 全选
    checkAllVal() {
      this.checkBoxList = this.checkAllVal ? this.listData : [];
    },
    // 高亮模式
    highlightModeSettings: {
      deep: true,
      handler: function() {
        this.getListData(this.data, { needCollection: false });
      },
    },
  },
  mounted() {
    this.getListData(this.data);
  },
};
</script>

<style lang="scss" scoped>
.list-wrap {
  position: relative;
  .list {
    position: relative;
    width: 100%;
    .tit {
      position: relative;
      display: flex;
      width: 100%;
      height: 41px;
      line-height: 41px;
      margin-top: 0;
      margin-bottom: 0;
      font-size: 14px;
      font-weight: bold;
      color: $color10;
      background-color: $color154;
      span {
        flex: 1;
        margin-right: 10px;
        text-align: center;
        font-size: 12px;
        &.space {
          flex: none;
          width: 35px;
        }
        &.No {
          flex: none;
          width: 40px;
          margin-right: 10px;
        }
      }
      .text-left {
        text-align: left;
      }
    }
    .context {
      position: relative;
      width: 100%;
      margin-top: 0;
      margin-bottom: 0;
      list-style: none;
      padding-left: 0;
      li {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        min-height: 60px;
        border-bottom: 1px solid $color2;
        box-sizing: border-box;
        background-color: $color2 !important;
        // &:nth-of-type(2n-1) {
        //   background-color: $color4;
        // }
        &.no-data {
          background-color: #fff;
        }
        p {
          position: relative;
          flex: 1;
          line-height: 1.5;
          padding: 12px 0;
          margin-top: 0;
          margin-bottom: 0;
          margin-right: 10px;
          text-align: center;
          font-size: 12px;
          font-weight: 700;
          color: $color150;
          word-break: break-all;
          word-wrap: break-word;
          > span {
            font-size: 12px;
          }
          &:nth-of-type(1) {
            text-align: left;
          }
          &.space {
            flex: none;
            width: 35px;
            text-align: right;
            .list-checkbox {
              position: absolute;
              top: 2px;
              right: -7px;
              padding-left: 12px;
            }
          }
          &.No {
            flex: none;
            width: 40px;
            margin-right: 10px;
            color: $color6;
          }
          &.ttl {
            text-align: left;
            > span {
              cursor: pointer;
            }
          }
          &.ipc {
            text-align: left;
          }
          &.text-left {
            text-align: left;
          }
          .status {
            display: inline-block;
            min-width: 44px;
            line-height: 26px;
            border-radius: 3px;
            box-sizing: border-box;
            margin: 0 5px;
            font-size: 12px;
            font-weight: normal;
            color: $color2;
            background-color: $mainColor;
          }
        }
      }
    }
  }
}

.pn-info {
  position: relative;
  span {
    display: inline-block;
    min-height: 30px;
    line-height: 30px;
    margin-right: 5px;
    vertical-align: middle;
    cursor: pointer;
    &.icon {
      display: inline-block;
      width: 24px;
      margin-top: -2px;
      margin-right: 5px;
      text-align: center;
      font-weight: normal;
      font-size: 16px !important;
      cursor: pointer;
    }
    &.iconzhankai2 {
      margin-top: 0;
      font-size: 20px !important;
    }
    &.iconshoucang11 {
    }
    &.iconshoucang3 {
      color: $mainColor;
    }
  }
}
// .sp1 {
//   flex: none !important;
//   min-width: 140px;
// }
// .sp2 {
//   flex: none !important;
//   width: 14%;
//   min-width: 160px;
// }
// .sp3 {
//   flex: none !important;
//   width: 10%;
//   max-width: 140px;
// }
.no-data {
  position: relative;
  line-height: 50px;
  text-align: center;
  font-size: 14px;
  color: $color6;
  background-color: $color2;
}
</style>
